using System;
using System.Collections.Generic;
using System.Text;
using NBusiness.Query;

namespace NBusiness.MySql.Query
{
    public class MySqlColumn : IColumn
    {
        #region Member variables and properties
        private string _name;
        private string _alias;
        #endregion

        #region Constructors
        public MySqlColumn() { }
        public MySqlColumn(string name)
            : this(name, null, null)
        { }
        public MySqlColumn(string name, ITable parent)
            : this(name, null, parent)
        { }
        public MySqlColumn(string name, string alias)
            : this(name, alias, null)
        { }
        public MySqlColumn(string name, string alias, ITable parent)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            _name = name;
            _alias = alias;
            _parent = parent;
        }
        #endregion

        #region IColumn Members

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Alias
        {
            get { return _alias; }
            set { _alias = value; }
        }

        #endregion

        #region IRelationalElement Members

        public string ToSql()
        {
            StringBuilder builder = new StringBuilder();
            if (Parent != null && Parent is ITable)
            {
                if ((Parent as ITable).Alias != null)
                {
                    builder.Append("`" + (Parent as ITable).Alias + "`.");
                }
                else
                {
                    builder.Append("`" + (Parent as ITable).Name + "`.");
                }
            }
            builder.Append("`" + _name + "`");

            if (_alias != null)
                builder.Append(" AS `" + _alias + "`");

            return builder.ToString();
        }

        private ITable _parent;
        public ITable Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        #endregion

        #region Operator overloads
        #region Greater Than
        public static MySqlBoolean operator >(MySqlColumn column, object value)
        {
            MySqlGreaterThan gt = new MySqlGreaterThan();
            gt.Left = column;
            LoadRight(gt, value);
            return gt;
        }
        public static MySqlBoolean operator >=(MySqlColumn column, object value)
        {
            MySqlGreaterThan gt = new MySqlGreaterThan();
            gt.OrEqual = true;
            gt.Left = column;
            LoadRight(gt, value);
            return gt;
        }
        #endregion

        #region Less Than
        public static MySqlBoolean operator <(MySqlColumn column, object value)
        {
            MySqlLessThan lt = new MySqlLessThan();
            lt.Left = column;
            LoadRight(lt, value);
            return lt;
        }
        public static MySqlBoolean operator <=(MySqlColumn column, object value)
        {
            MySqlLessThan lt = new MySqlLessThan();
            lt.OrEqual = true;
            lt.Left = column;
            LoadRight(lt, value);
            return lt;
        }
        #endregion

        #region Equality
        //public static bool operator ==(SqlColumn c1, SqlColumn c2)
        //{
        //    return c1.Equals(c2);
        //}
        //public static bool operator !=(SqlColumn c1, SqlColumn c2)
        //{
        //    return c1.Equals(c2);
        //}
        public static MySqlEqual operator ==(MySqlColumn column, object value)
        {
            MySqlEqual equal = new MySqlEqual();
            equal.Left = column;
            LoadRight(equal, value);
            return equal;
        }

        public static MySqlEqual operator !=(MySqlColumn column, object value)
        {
            MySqlEqual equal = column == value;
            equal.Not = true;
            return equal;
        }
        #endregion 

        public static MySqlBoolean operator %(MySqlColumn column, string value)
        {
            MySqlLike like = new MySqlLike();
            like.Left = column;
            LoadRight(like, value);
            return like;
        }

        private static void LoadRight(MySqlBoolean expression, object value)
        {
            if (value is IEvaluatable)
                expression.Right = value as IEvaluatable;
            else if (value is string && !string.IsNullOrEmpty((string)value) && ((string)value).StartsWith("@"))
                expression.Right = new MySqlParameter((string)value);
            else
                expression.Right = new MySqlPrimitive(value);
        }
        #endregion
    }
}
